package com.thinkfit.jy.presenter;

import com.common.lib.bean.SportInfoBean;
import com.common.lib.bean.SportRecordBean;
import com.common.lib.bean.UserBean;
import com.common.lib.bean.VideoBean;
import com.common.lib.manager.DataManager;
import com.common.lib.mvp.BasePresenter;
import com.common.lib.network.HttpListener;
import com.common.lib.network.HttpMethods;
import com.common.lib.network.HttpObserver;
import com.thinkfit.jy.contract.SportRecordContract;
import com.thinkfit.jy.utils.DeviceTypeUtils;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;

public class SportRecordPresenter extends BasePresenter<SportRecordContract.View> implements SportRecordContract.Presenter {

    public SportRecordPresenter(@NotNull SportRecordContract.View rootView) {
        super(rootView);
    }

    @Override
    public void getSportInfo() {
        HttpMethods.Companion.getInstance().getSportInfo(
                DataManager.Companion.getInstance().getMyInfo().getUid(),
                new HttpObserver(false, getRootView(), new HttpListener<SportInfoBean>() {
                    @Override
                    public void onSuccess(@Nullable SportInfoBean bean) {
                        if (getRootView() == null) {
                            return;
                        }
                        getRootView().getSportInfoSuccess(bean);
                    }

                    @Override
                    public void dataError(@Nullable int code, @Nullable String msg) {
                        if (getRootView() == null) {
                            return;
                        }
                    }

                    @Override
                    public void connectError(@Nullable Throwable e) {
                        if (getRootView() == null) {
                            return;
                        }
                    }
                }, getCompositeDisposable()));
    }

    @Override
    public void getSportRecord(int page) {
        HttpMethods.Companion.getInstance().getSportRecord(
                DataManager.Companion.getInstance().getMyInfo().getUid(),
                page,
                new HttpObserver(false, getRootView(), new HttpListener<ArrayList<SportRecordBean>>() {
                    @Override
                    public void onSuccess(@Nullable ArrayList<SportRecordBean> list) {
                        if (getRootView() == null) {
                            return;
                        }
                        getRootView().getSportRecordSuccess(page, list);
                    }

                    @Override
                    public void dataError(@Nullable int code, @Nullable String msg) {
                        if (getRootView() == null) {
                            return;
                        }
                        getRootView().getSportRecordFailed();
                    }

                    @Override
                    public void connectError(@Nullable Throwable e) {
                        if (getRootView() == null) {
                            return;
                        }
                        getRootView().getSportRecordFailed();
                    }
                }, getCompositeDisposable()));
    }

    @Override
    public void getVideoCourse(int type,int page){
        UserBean myInfo = DataManager.Companion.getInstance().getMyInfo();
        HttpMethods.Companion.getInstance().videoList(
                myInfo == null ? "" : myInfo.getUid(),
                DeviceTypeUtils.getDeviceType(),type, 0, 0,0,0, 1,page,
                new HttpObserver(false, getRootView(), new HttpListener<ArrayList<VideoBean>>() {
                    @Override
                    public void onSuccess(@Nullable ArrayList<VideoBean> list) {
                        if (getRootView() == null) {
                            return;
                        }
//                        if (page == 1) {
//                            DataManager.Companion.getInstance().saveVideoList(type, index, list);
//                        }
                        getRootView().getVideoCourseSuccess(page, list);
                    }

                    @Override
                    public void dataError(@Nullable int code, @Nullable String msg) {
                        if (getRootView() == null) {
                            return;
                        }
                        getRootView().getVideoCourseFailed();
                    }

                    @Override
                    public void connectError(@Nullable Throwable e) {
                        if (getRootView() == null) {
                            return;
                        }
                        getRootView().getVideoCourseFailed();
                    }
                }, getCompositeDisposable()));
    }

}
